package com.jacobson.huobi.mvp

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.jacobson.core.base.Immersive
import com.jacobson.core.di.components.AppComponent
import com.jacobson.core.di.modules.ActivityModule
import com.jacobson.core.mvp.BaseMvpActivity
import com.jacobson.huobi.R
import com.jacobson.huobi.components.SpaceItemDecoration
import com.jacobson.huobi.di.components.DaggerInviteUserListActivityComponent
import com.jacobson.huobi.di.modules.InviteUserListModule
import com.jacobson.huobi.entities.InvitedNumberInfo
import com.jacobson.huobi.entities.response.CommonResponse
import com.jacobson.huobi.entities.response.InviteUserPageResponse
import com.jacobson.huobi.mvp.adapters.InviteUserListAdapter
import com.jacobson.huobi.mvp.contract.InviteDetailsContract
import com.jacobson.huobi.utils.ResourcesUtil
import kotlinx.android.synthetic.main.activity_invitation_details.*
import kotlinx.android.synthetic.main.base_title_bar.*
import javax.inject.Inject

@Immersive(fitSystemWindows = false)
class InviteUserListActivity :
    BaseMvpActivity<InviteDetailsContract.Presenter>(), InviteDetailsContract.View {

    private var lastTs: Int = 0

    @field:Inject
    lateinit var inviteUserListAdapter: InviteUserListAdapter

    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerInviteUserListActivityComponent.builder()
            .inviteUserListModule(InviteUserListModule(this))
            .appComponent(appComponent)
            .activityModule(ActivityModule(this))
            .build()
            .inject(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //修改底部功能键背景色 只支持api为21及以上
        if (Build.VERSION.SDK_INT  >= Build.VERSION_CODES.LOLLIPOP) {
            window.navigationBarColor = ResourcesUtil.getColor(R.color.colorPrimary) //设定底部导航栏  同全局颜色
        }
    }

    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_invitation_details
    }

    override fun initData(savedInstanceState: Bundle?) {

        tvTitle.text = "邀请明细"

        with(mRecyclerView) {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(3f))
            adapter = inviteUserListAdapter
        }
        with(mSmartRefreshLayout) {
            setOnRefreshListener {
                loadInviteUserList(true)
            }
            setOnLoadMoreListener {
                loadInviteUserList(false)
            }
        }

        loadInviteUserList(true)
        presenter.getSharedInfo()
    }

    override fun onInviteUserResp(response: InviteUserPageResponse) {
        mSmartRefreshLayout.finishLoadMore()
        mSmartRefreshLayout.finishRefresh()
        if (response.curPage == 0) {
            lastTs = response.curPage
            inviteUserListAdapter.update(response.data)
        } else if (lastTs == response.curPage) {
            inviteUserListAdapter.append(response.data)
        }
        if (response.curPage + 1 < response.pageSize) {
            mSmartRefreshLayout.setEnableLoadMore(true)
        } else {
            mSmartRefreshLayout.setEnableLoadMore(false)
        }
    }

    override fun onSharedInfoResp(response: CommonResponse<InvitedNumberInfo>) {
        tvInvitationNumber.text = "已邀请人数：${response.data.oneLevelTotal}"
    }

    private fun loadInviteUserList(refresh: Boolean = false) {
        if (refresh) {
            lastTs = 0
        }
        presenter.loadInviteUserList(lastTs)
    }

    override fun onError(type: Int, extra: Any?, code: Int, message: String): Boolean {
        mSmartRefreshLayout.finishLoadMore()
        mSmartRefreshLayout.finishRefresh()
        logger.e("error >>> $message")
        return super.onError(type, extra, code, message)
    }

    companion object {
        fun startActivity(context: Context) {
            Intent(context, InviteUserListActivity::class.java).run {
                context.startActivity(this)
            }
        }
    }
}